<?php
namespace app\admin\controller;
use app\admin\model\Member;
use think\Cache;
use think\Db;

/**
 * 财务管理
 * Class Member
 */
class FinanceController extends BaseController {

    protected function _initialize() {
        parent::_initialize();
    }

	/**
	 * 财务统计
	 */
	public function index() {
		//今日收款金额
		$jtskze = db('paylog')->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jtskze',$jtskze);		
		
		//今日付款人数
		$jtfkrs = db('paylog')->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jtfkrs',$jtfkrs);
		
		//今日付款笔数
		$jtfkbs = db('paylog')->whereTime('add_time', 'today')->count();
		$this->assign('jtfkbs',$jtfkbs);
		
		//收款总金额
		$skze = db('paylog')->sum('balance');
		$this->assign('skze',$skze);		
		
		//付款人数
		$fkrs = db('paylog')->group('user_id')->count();
		$this->assign('fkrs',$fkrs);
		
		//付款笔数
		$fkbs = db('paylog')->count();
		$this->assign('fkbs',$fkbs);
		
		
		//今日发放金额
		$jtffze = db('gdyj_record')->whereTime('add_time', 'today')->sum('balance');
		$this->assign('jtffze',$jtffze);		
		
		//今日发放人数
		$jtffrs = db('gdyj_record')->whereTime('add_time', 'today')->group('user_id')->count();
		$this->assign('jtffrs',$jtffrs);
		
		//今日发放笔数
		$jtffbs = db('gdyj_record')->whereTime('add_time', 'today')->count();
		$this->assign('jtffbs',$jtffbs);
		
		//发放总金额
		$ffze = db('gdyj_record')->sum('balance');
		$this->assign('ffze',$ffze);		
		
		//发放人数
		$ffrs = db('gdyj_record')->group('user_id')->count();
		$this->assign('ffrs',$ffrs);
		
		//发放笔数
		$ffbs = db('gdyj_record')->count();
		$this->assign('ffbs',$ffbs);
		return $this->fetch();
	}
	/**
	 * 昨天发放金额、累计发放金额、待发放金额、
	 * 佣金发放记录（发放商品名称、时间、金额、会员昵称），
	 * 按时间、按金额、按名称可以搜索
	 */
	public function yj_tj(){
		$data['yes']=db('gdyj_record')->wheretime("add_time","yesterday")->cache(30)->sum('balance');
		$data['today']=db('gdyj_record')->wheretime("add_time","today")->cache(30)->sum('balance');
		
		
	}
	//佣金日志
	public function yj_logs(){
		$map = [];
		if(input("keyword")){
			$map['c.id|c.mobile|c.real_name'] = ['like','%'.input("keyword").'%'];
		}
		if(input('begin_time') && input('end_time')){
			$map['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
			$this->assign('begin_time', input('begin_time'));
			$this->assign('end_time', input('end_time'));
		}elseif(input("begin_time")){
			$map['a.add_time']=['>=',"".input("begin_time").""];
			$this->assign('begin_time',input("begin_time"));
			$this->assign('end_time','');			
		}elseif(input("end_time")){
			$map['a.add_time']=['<=',"".input("end_time").""];
			$this->assign('begin_time','');		
			$this->assign('end_time',input("end_time"));			
		}else{
			$this->assign('begin_time','');	
			$this->assign('end_time','');		
		}
        $list = db('gdyj_record')->alias("a")
        ->join("member c","a.user_id=c.id","left")
        ->join("member m","a.pt_id=m.id","left")
        ->join("member b","a.city_id=b.id","left")
        ->where($map)->field("a.*,m.mobile,m.real_name as ptname,m.nick_name as ptname2,b.nick_name as cityname2,b.real_name as cityname,c.real_name")
        ->order("a.add_time desc")->paginate(15, false, get_query());
        return $this->fetch('', ['list' => $list]);
    
	}
	/**
     * 佣金提现管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function present($keyword = '', $ziduan = 'id', $param='create_time', $sort='desc') {
        $map = [];
		$where = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
		
		if(input('status')&&input('status')!=4){
			$map['a.status'] = input('status');
			$where['status'] = input('status');
		}elseif(input('status')==4){
			
		}else{
			$map['a.status'] = 1;
		}
        $tixian_list = db('member_tixian')->alias('a')
					->join('member b','a.user_id = b.id','LEFT')
					->field('a.*,b.nick_name,b.mobile')
        			->where($map)->order($order)->paginate(15, false, get_query());
	   	$count = db('member_tixian')->where($where)->count();
		$this->assign('count', $count);
	    return $this->fetch('present', ['tixian_list' => $tixian_list, 'keyword' => $keyword]);
    }
	
	//佣金提现审核
	public function shenghe() {
    	if($_POST){
			$parm['status']= 2;
			$result = db('member_tixian')->where('id',input('id'))->update($parm);
	        if($result){
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	//佣金提现驳回
	public function bohui() {
    	if($_POST){
    		$txinfo = db('member_tixian')->where('id',input('id'))->find();
			
			//修改提现状态
       	 	$parm['status'] = 3;  
       	 	$result = db('member_tixian')->where('id',input('id'))->update($parm);
			
			//操作用户佣金		
			$user_info = db('member')->where('id',input('user_id'))->find();
	        if($result){
		        gdyj_record_log($user_info['id'],$txinfo['money'],"提现审核驳回,佣金返还",18,'',$user_info['pt_id'],$user_info['city_id']);
				//扣除佣金
				$rest = db('member')->where('id', $user_info['id'])->setInc('yj', $txinfo['money']);
				return json(['code'=>200,'msg'=>'操作成功']);
	        }else{
	       		return json(['code'=>402,'msg'=>'操作失败']);
	        }
        }
	}
	
	//收款日志
	public function sk_logs(){
		$map = [];
		if(input("keyword")){
			$map['c.id|c.mobile|c.real_name'] = ['like','%'.input("keyword").'%'];
		}
		if(input("order_type")){
			$map['a.order_type'] = input("order_type");
		}
		if(input('begin_time') && input('end_time')){
			$map['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
			$this->assign('begin_time', input('begin_time'));
			$this->assign('end_time', input('end_time'));
		}elseif(input("begin_time")){
			$map['a.add_time']=['>=',"".input("begin_time").""];
			$this->assign('begin_time',input("begin_time"));
			$this->assign('end_time','');			
		}elseif(input("end_time")){
			$map['a.add_time']=['<=',"".input("end_time").""];
			$this->assign('begin_time','');		
			$this->assign('end_time',input("end_time"));			
		}else{
			$this->assign('begin_time','');	
			$this->assign('end_time','');		
		}
		$map['a.state'] = 2;
        $list = db('paylog')->alias("a")
        ->join("member c","a.user_id=c.id","left")
        ->join("member m","a.pt_id=m.id","left")
		->join("member b","a.city_id=b.id","left")
        ->where($map)->field("a.*,m.mobile,m.real_name as ptname,b.real_name as cityname,c.real_name")
        ->order("a.add_time desc")->paginate(15, false, get_query());
        return $this->fetch('', ['list' => $list]);
    
	}

	//佣金变化记录
	public function show_yj() {  
   	 	$result = db('gdyj_record')->where('user_id',input('user_id'))->order('add_time desc')->paginate(15, false, get_query());
		$this->assign('result',$result);	
 		return $this->fetch();
	}
}
